package com.warm.androidstudy.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.text.TextUtils;
import android.util.ArrayMap;


import com.warm.androidstudy.config.AllUtilConfig;

import java.util.Iterator;
import java.util.Locale;
import java.util.Map;


/**
 * Created by admin on 2017/12/28.
 */
public class NetUtils {
    private final String URL_SPLITTER = "/";

    private NetUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static boolean isConnected(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService("connectivity");
        if(null != connectivity) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if(null != info && info.isConnected() && info.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }

        return false;
    }

    public static boolean isWifi(Context context) {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService("connectivity");
        return cm == null?false:cm.getActiveNetworkInfo().getType() == 1;
    }

    public static String Judgeurl(String weburl) {
        if(weburl == null) {
            return "";
        } else {
            if(weburl.length() > 0 && !weburl.toLowerCase(Locale.ENGLISH).trim().contains("http://") && !weburl.toLowerCase(Locale.ENGLISH).trim().contains("https://")) {
                weburl = AllUtilConfig.Host_IP + weburl;
            }

            return Uri.decode(weburl);
        }
    }

    public static String Judgeurl(String weburl, String Host) {
        if(weburl == null) {
            return "";
        } else {
            if(weburl.length() > 0 && !weburl.toLowerCase(Locale.ENGLISH).trim().contains("http://") && !weburl.toLowerCase(Locale.ENGLISH).trim().contains("https://")) {
                weburl = Host + weburl;
            }

            return Uri.decode(weburl);
        }
    }

    public static String UrlBuide(String mUrl, Map<String, String> mParamsMap) {
        Uri.Builder builder = Uri.parse(mUrl).buildUpon();
        Iterator var3 = mParamsMap.keySet().iterator();

        while(var3.hasNext()) {
            String key = (String)var3.next();
            String value = (String)mParamsMap.get(key);
            builder.appendQueryParameter(key, value);
        }

        return builder.toString();
    }

    //拼接URL的各种入参方法
    public static String UrlBuilder(String mUrl, ArrayMap<String, String> mParamsMap) {
        Uri.Builder builder = Uri.parse(mUrl).buildUpon();
        if(mParamsMap != null && mParamsMap.size() > 0) {
            for(int i = 0; i < mParamsMap.size(); ++i) {
                String curKey = (String)mParamsMap.keyAt(i);
                String curValue = (String)mParamsMap.valueAt(i);
                if(!TextUtils.isEmpty(curKey) && curValue != null) {
                    builder.appendQueryParameter(curKey, curValue);
                }
            }
        }

        return builder.toString();
    }
}
